home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / os2 / bltc127.zip / BLTXMPLS.ZIP / B2_X01.C < prev    next >
Text File  |  1995-02-08  |  8KB  |  238 lines

  1.  
  2.  
  3. /* this is a temporary release sample to read "any" DBF, or at least describe the
  4.  * concept -- use the included ACPART.DBF as test input (command line arg) since
  5.  * it is small and everything fits on screen
  6.  *
  7.  * this source is being done in OS/2, so remove/add headers as required 
  8.  * also, you should be able to use your current bullet.h, though you may
  9.  * want to modify/adapt to the one included with this temporary release
  10.  * -- see #include "bullet2.h"  and  #define BULLET BULLET32  below
  11.  * -- (you will need to remove the #define BULLET BULLET32)
  12.  */
  13.  
  14. #include <os2.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18.  
  19. /* if 16-bit program, use:                                        
  20.  *   #define __Bullet16
  21.  *   #include "bullet2.h
  22.  * otherwise (32-bit), do as below
  23.  */
  24.  
  25. #include "bullet2.h"
  26. #define BULLET BULLET32   /* if 32-bit main(), go through BULLET32 */
  27.  
  28. #define ITOA itoa          /* non-standard */
  29.  
  30. /* b2_xvue.c:
  31.   1) viewing any DBF by building field list on-the-fly
  32.   2) extended limits (255 fields, 8KB records, binary fields)
  33.  
  34.  */
  35.  
  36. #pragma pack(1)
  37.  
  38. struct InitPack IP;
  39. struct ExitPack EP;
  40. struct FieldDescType fieldList;
  41. struct DescriptorPack DP;
  42. struct CreateDataPack CDP;
  43. struct CreateKeyPack CKP;
  44. struct OpenPack OP;
  45. struct HandlePack HP;
  46. struct StatDataPack SDP;
  47. struct AccessPack AP;
  48. struct DosFilePack DFP;
  49.  
  50. char  dataRec[8192];       /* unknown record layout since reading "any" DBF */
  51. char  *Ptmp2;              /* point into data record, start of fields */
  52.  
  53. /* any Bullet-used structures should be within the pack(1)/pack() pragmas */
  54. /* these vars are up above main() only because I didn't care? move/adapt as you want */
  55.  
  56. #pragma pack()
  57.  
  58. int     rez;
  59. int     rez2;
  60.  
  61. typedef struct _DATADESC {
  62.    char fieldName[11];     /* our program is using this structure to copy the DBF */
  63.    char fieldType;         /* field descriptors to -- could also use FieldDescType, */
  64.    BYTE fieldLen;          /* but since this is only being used by our program, the */
  65.    BYTE fieldDC;           /* 14 bytes is less than half the size of FieldDescType */
  66. } DATADESC;
  67. typedef DATADESC *PDATADESC;  /* short descriptor data structure */
  68. /* not directly accessed by Bullet, so need not be in above pack(1)/pack() */
  69.  
  70. PDATADESC  PdataDesc;   /* pointer to field descriptor base (the first one) */
  71. PDATADESC  Ptmp;        /* as above but varies, locating to any field's descriptor */
  72.  
  73. char     userDBF[260];  /* pathname of DBF */
  74. char     fmt[32];       /* printf() fmt string */
  75. char     tmpBuff[32];   /* misc buffer for itoa() amd other string builds */
  76. USHORT   handleData;    /* handle of DBF */
  77. LONG     recNo;         /* loop counter */
  78. BYTE     fldNo;         /* loop counter */
  79. USHORT   offset;        /* index into record of field */
  80.  
  81.  
  82. int main(int argc,char *argv[]) {
  83.  
  84. setbuf(stdout,NULL);
  85.  
  86. if (argc < 2) {
  87.    puts("gimme something to access...");
  88.    return(1);  /* for now */
  89. }
  90.  
  91. /* init/open existing DBF as passed on command line */
  92.  
  93. IP.func = INITXB;
  94. IP.JFTmode = 0;
  95. rez = BULLET(&IP);
  96. if (rez!=0) {
  97.    printf("InitXB failed: %u\n",rez);
  98.    return(1);
  99. }
  100.  
  101. OP.func = OPENDXB;
  102. OP.filenamePtr = argv[1];
  103. OP.asMode = READWRITE | DENYNONE;
  104. rez = BULLET(&OP);
  105. if (rez==0) {
  106.  
  107.    handleData = OP.handle;
  108.    SDP.func = STATDXB;
  109.    SDP.handle = handleData;
  110.    rez = BULLET(&SDP);
  111.    if (rez==0) {
  112.  
  113.       /* allocate field descriptors used by program 
  114.        * -- not full 32 bytes per descriptor, but only 14 bytes per desc
  115.        * -- use calloc() since want 0-filled storage
  116.        */
  117.  
  118.       PdataDesc = calloc(SDP.fields,sizeof(DATADESC));
  119.  
  120.       if (PdataDesc!=NULL) {
  121.          Ptmp = PdataDesc;
  122.  
  123.          /* read each field descriptor from Bullet, storing to our program */
  124.          /* show each for educatoinal reasons */
  125.  
  126.          /*    1234567890-123456789-123456789- */
  127.          puts("FLD#   FIELDNAME  T  LEN.DEC");
  128.  
  129.          DP.func = GETDESCRIPTORXB;
  130.          DP.handle = handleData;
  131.          for (fldNo=1;fldNo <= SDP.fields;fldNo++) {
  132.  
  133.             DP.fieldNumber = fldNo;
  134.             rez = BULLET(&DP);
  135.             if (rez==0) {
  136.  
  137.                strcpy(Ptmp->fieldName,DP.FD.fieldName);
  138.                Ptmp->fieldType = DP.FD.fieldType;
  139.                Ptmp->fieldLen = DP.FD.fieldLen;
  140.                Ptmp->fieldDC = DP.FD.fieldDC;
  141.                printf("%3u   %10s  %c  %3u.%1u\n",
  142.                   fldNo,
  143.                   Ptmp->fieldName,
  144.                   Ptmp->fieldType,
  145.                   Ptmp->fieldLen,
  146.                   Ptmp->fieldDC);
  147.                Ptmp++;
  148.             }
  149.             else
  150.                break;
  151.          }
  152.       
  153.          /* okay, have all we need to know about the DBF fields */
  154.          /* grab the first few records and spit them out, by field */
  155.  
  156.          if (SDP.recs != 0) {
  157.  
  158.             AP.func = GETRECORDXB;  /* no index involved in this example - going */
  159.             AP.handle = handleData; /* directly to the DBF by recno to show one way */
  160.             AP.recPtr = &dataRec;   /* of processing a DBF file on-the-fly */
  161.  
  162.             for (recNo=1;recNo <= 10; recNo++) {
  163.  
  164.                printf("\nrecNo %u: ",recNo);
  165.  
  166.                AP.recNo = recNo;      /* get this record number (to dataRec) */
  167.                rez = BULLET(&AP);
  168.                if (rez==0) {
  169.  
  170.                   Ptmp = PdataDesc;  /* start at first field descriptor */
  171.                   offset = 1;         /* index of first field, skipping tag for now */
  172.  
  173.                   for (fldNo=1;fldNo <= SDP.fields;fldNo++) {
  174.  
  175.                      switch (Ptmp->fieldType) {
  176.                         case 'B':                     /* special Bullet binary */
  177.                            printf("% 10.10u",(int *) Ptmp2);/* assume int, 32-bit */
  178.                            break;                     /* I think (int *) is correct? */
  179.                         case 'C':                     /* text */
  180.                         case 'D':                     /* date, show as-is */
  181.                         case 'L':                     /* logical, show as-is */
  182.                         case 'M':                     /* memo field (offset in ASCII) */
  183.                         case 'N':                     /* numeric (ASCII) */
  184.                         /* you probably want to replace this with sprintf() */
  185.                            strcpy(fmt," %");          /* first build format string */
  186.                            ITOA(Ptmp->fieldLen,tmpBuff,10); /* xx */
  187.                            strcat(fmt,tmpBuff);       /* %xx */
  188.                            strcat(fmt,".");           /* %xx. */
  189.                            strcat(fmt,tmpBuff);       /* %xx.xx */
  190.                            strcat(fmt,"s");           /* %xx.xxs */
  191.  
  192.                            Ptmp2 = &dataRec;                       
  193.                            Ptmp2 += offset;  /* locate to field */
  194.                            printf(fmt,Ptmp2);
  195.                            break;
  196.                         default:
  197.                            printf("\nUnknown field type found: %c\n",Ptmp->fieldType);
  198.                      } /* switch */
  199.  
  200.                      offset += Ptmp->fieldLen;  /* next field's data start */
  201.                      Ptmp += 1;                 /* next field descriptor */
  202.  
  203.                   } /* for fields */
  204.                } /* if record read */
  205.  
  206.                else {
  207.                   printf(" - failed GetRecordXB, stat: %u (1224=no more records)\n",rez);
  208.                   break;  /* error probably just EOF (i.e., access attempt beyond last rec) */
  209.                }
  210.  
  211.             } /* for records */
  212.             puts("\nThat's all folks!"); /* all FOR recs printed, do new line/okay */
  213.          }
  214.          else
  215.             puts("No records in file");
  216.  
  217.          free(PdataDesc);
  218.       }
  219.       else
  220.          puts("calloc failed!");
  221.    }
  222.    else
  223.       printf("StatDXB failed: %u\n",rez);
  224. }
  225. else
  226.    printf("OpenXB failed: %u\n",rez);
  227.  
  228. EP.func = EXITXB;
  229. rez2=BULLET(&EP);    /* return main rez, not the rather uninteresting ExitXB */
  230.  
  231. printf("\nPress ENTER...");
  232. getchar();
  233.  
  234. return(!rez==0);
  235. }
  236.  
  237.  
  238.